package breeze.tool.life.broadcasts;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import breeze.tool.life.R;
import android.appwidget.AppWidgetProvider;
import android.widget.RemoteViews;
import android.content.res.AssetManager;
import breeze.tool.life.services.WeatherUpdateServics;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.content.ComponentName;
import breeze.tool.life.module.WeatherNowBean;
import android.graphics.BitmapFactory;
import java.io.IOException;
import brz.breeze.service_utils.BExceptionCatcher;
import breeze.tool.life.App;
import brz.breeze.app_utils.BAppUtils;
import brz.breeze.tool_utils.Blog;
import android.appwidget.AppWidgetManager;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import breeze.tool.life.utils.AppWeatherUtils;

public class WeatherWidget extends AppWidgetProvider {
    /*
	 *@author BREEZE
	 *@date 2021-11-28 10:33:36
	 */

    public static final String TAG = "WeatherWidget";

	private static WeatherUpdateServics update;

	private ServiceConnection ServiceCon;

	private SharedPreferences sp;

    @Override
    public void onReceive(Context context, Intent intent) {
        onEnabled(context);
    }

	@Override
	public void onEnabled(Context context) {
		super.onEnabled(context);
		init(context);
	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		init(context);
	}

	private void init(final Context context) {
		sp = PreferenceManager.getDefaultSharedPreferences(context);
		ServiceCon = new ServiceConnection(){
			@Override
			public void onServiceConnected(ComponentName p1, IBinder p2) {
				WeatherUpdateServics.MBinder binder = (WeatherUpdateServics.MBinder) p2;
				update = binder.getService();
				BAppUtils.execute(new Runnable(){
						public void run() {
							WeatherNowBean bean = update.getBean();
							if (bean != null) {
								if (bean.getCode().equalsIgnoreCase("200")) {
									WeatherNowBean.Now bean_now = bean.getNow();
									String icon = bean_now.getIcon();
									RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.layout_widget_weather);
									AppWidgetManager awm = AppWidgetManager.getInstance(context);
									rv.setImageViewBitmap(R.id.layoutwidgetweather_icon, AppWeatherUtils.getWeatherIcon(context, icon));
									rv.setTextViewText(R.id.layoutwidgetweather_temp, bean_now.getTemp() + "℃");
									rv.setTextViewText(R.id.layoutwidgetweather_weather, bean_now.getText());
									StringBuilder sb = new StringBuilder();
									sb.append(bean_now.getWindDir()).append("\n")
										.append(bean_now.getWindScale() + "级");
									rv.setTextViewText(R.id.layoutwidgetweather_cityname, sp.getString("city_name", "成都"));
									rv.setTextViewText(R.id.layoutwidgetweather_more, sb);
									ComponentName cn = new ComponentName(context, WeatherWidget.class);
									awm.updateAppWidget(cn, rv);
								}
							}
						}
					});
			}

			@Override
			public void onServiceDisconnected(ComponentName p1) {
			}
		};
		Intent service = new Intent(context, WeatherUpdateServics.class);
		context.startService(service);
		context.getApplicationContext().bindService(service, ServiceCon, 1);
	}

}
